package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.CheckGroup;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查组的控制层
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;


    /**
     * 添加检查组
     *
     * @param checkGroup
     * @param checkitemIds
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupService.add(checkGroup, checkitemIds);
        return Result.success(MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 检查组分页查询
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = checkGroupService.findPage(queryPageBean);
        return Result.success(MessageConstant.QUERY_CHECKGROUP_SUCCESS, page);
    }

    /**
     * 根据id查询检查组
     *
     * @param id
     * @return
     */
    @PostMapping("/findById")
    public Result findById(Integer id) {
        CheckGroup checkGroup = checkGroupService.findById(id);
        return Result.success(MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }

    /**
     * 根据检查组ID查询检查项ID
     *
     * @param id
     * @return
     */
    @PostMapping("/findCheckItemIds")
    public Result findCheckItemIds(Integer id) {
        Integer[] checkItemIds = checkGroupService.findCheckItemIds(id);
        return Result.success(MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemIds);
    }

    /**
     * 修改检查组
     *
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupService.edit(checkGroup, checkitemIds);
        return Result.success(MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 删除检查组
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteCheckGroup")
    public Result deleteCheckGroup(Integer id) {
        try {
            checkGroupService.deleteCheckGroup(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return Result.success(MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询所有检查组用于套餐项目展示
     *
     * @return
     */
    @PostMapping("/findAll")
    public Result findAll() {
        try {
            List<CheckGroup> groupList = checkGroupService.findAll();
            return Result.success(MessageConstant.QUERY_CHECKGROUP_SUCCESS, groupList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
